package com.zry.simpleBlog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zry.simpleBlog.comment.aop.annotations.AuthCheck;
import com.zry.simpleBlog.comment.aop.annotations.LogWeb;
import com.zry.simpleBlog.comment.respBean.RespBean;
import com.zry.simpleBlog.dto.BlogQuery;
import com.zry.simpleBlog.entity.Blog;
import com.zry.simpleBlog.service.IBlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 */
@LogWeb(title = "博文操作类")
@RestController
public class BlogController {

    @Resource
    private IBlogService blogService;

    /**
     * 功能描述:根据博客id返回文章
     *
     * @param id 博客id
     * @CheckLogin(isLogin = false) 是指:
     * 将登录的用户信息加入UserContext中.
     * 若未登陆,UserContext中则为null.
     * @author zry
     * @create 2022/4/28
     */
    @LogWeb(title = "查询文章")
    @AuthCheck(isLogin = false)
    @GetMapping("blogs/{id}")
    public RespBean getBlog(@PathVariable String id) {
        return blogService.getBlogById(id);
    }

    /**
     * 首页文章展示分页
     *
     * @return mv
     */
    @Operation(summary = "文章展示分页", description = "文章展示分页: 可选参数[分类][标签],并且如果标签参数存在那么按照标签查询")
    @GetMapping("blogs")
    public RespBean blogPage(@Parameter(name = "pageSize", description = "页大小") @RequestParam(defaultValue = "8") Integer pageSize,
                             @Parameter(name = "pageNum", description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(required = false) String typeId,
                             @RequestParam(required = false) String tagId) {
        RespBean respBean;
        //只展示已发布的文章
        if (tagId == null) {
            BlogQuery query = new BlogQuery();
            query.setTypeId(typeId);
            respBean = blogService.blogPage(pageNum, pageSize, query);
        } else {
            respBean = blogService.blogPage(pageNum, pageSize, tagId);
        }
        return respBean;
    }

    /**
     * 推荐博客文章
     *
     * @return mv
     */
    @Cacheable(value = "Blog_Recommends")
    @Operation(summary = "推荐文章列表", description = "默认推荐文章展示前5个")
    @GetMapping("recommendBlogs")
    public RespBean recommendBlog(@RequestParam(defaultValue = "5") Integer num) {
        List<Blog> blogList = blogService.list(new QueryWrapper<Blog>().select("id", "title").eq("published", true).eq("recommend", true).orderByAsc("update_time").last("limit " + num));
        return RespBean.success(blogList);
    }

    @Operation(summary = "时间归档", description = "按时间倒序Map结构{key : year ，value: List<Blog>}")
    @GetMapping("archives")
    public RespBean archives() {
        return blogService.mapArchives();
    }

}
